package com.yeren.czbk.manyToMany;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

import com.yeren.czbk.manyToMany.entity.Student;
import com.yeren.czbk.manyToMany.entity.Teacher;



@SuppressWarnings("deprecation")
public class App {
	private static SessionFactory ssessionFactory;
	//初始化ssessionFactory
	static{
		Configuration cfg=new Configuration();
		cfg.configure("hibernate.cfg.xml");//读取配置文件
		ssessionFactory=cfg.buildSessionFactory();//根据配置生成session工厂
	}
//	@Test
	public void testSave() throws Exception{
		Session session=ssessionFactory.openSession();//打开一个新的事务
		Transaction t =session.beginTransaction();//开始事务
		//-------------------------------------------------
		Teacher teacher1=new Teacher();
		teacher1.setName("彭辉");
		Teacher teacher2=new Teacher();
		teacher2.setName("刘勇");
		
		Student student1=new Student();
		student1.setName("沈金浩");
		Student student2=new Student();
		student2.setName("王紫鹃");
		
		//关联起来,只需要一组就可以维护关联关系
//		student1.getTeachers().add(teacher1);
//		student1.getTeachers().add(teacher2);
//		student2.getTeachers().add(teacher1);
//		student2.getTeachers().add(teacher2);
		
		teacher1.getStudents().add(student1);
		teacher1.getStudents().add(student2);
		teacher2.getStudents().add(student1);
		teacher2.getStudents().add(student2);
		
		session.save(student1);
		session.save(student2);
		session.save(teacher1);
		session.save(teacher2);
		
		//-------------------------------------------------
		session.getTransaction().commit();
		session.close();//关闭session，释放资源
	}
	
	
//	@Test
	public void testGet() throws Exception{
		Session session=ssessionFactory.openSession();//打开一个新的事务
		Transaction t =session.beginTransaction();//开始事务
		//-------------------------------------------------
		Teacher teacher=(Teacher)session.get(Teacher.class, 3l);
		System.out.println(teacher);
		System.out.println(teacher.getStudents());
		
		
		//-------------------------------------------------
		session.getTransaction().commit();
		session.close();//关闭session，释放资源
	}
	
	//解除关联关系
//	@Test
	public void testRemoveRelation() throws Exception{
		Session session=ssessionFactory.openSession();//打开一个新的事务
		Transaction t =session.beginTransaction();//开始事务
		//-------------------------------------------------
		//如果inverse=false就可以解除，如果为true就不可以解除
		Teacher teacher=(Teacher)session.get(Teacher.class, 3l);
		teacher.getStudents().clear();
		
		
		//-------------------------------------------------
		session.getTransaction().commit();
		session.close();//关闭session，释放资源
	}
	
	//删除对象，对关联关系的影响
	@Test
	public void testDelete() throws Exception{
		Session session=ssessionFactory.openSession();//打开一个新的事务
		Transaction t =session.beginTransaction();//开始事务
		//-------------------------------------------------
		//删除一方
		//a,如果没有关联的对方，能删除。
		//b,如果有关联的对方且inverse=false,由于可以维护关联关系，他就会先删除关联关系，再删除自己
		//c,如果有关联的对方且inverse=true,由于不能维护关联关系，所以会直接执行删除，就会有异常
		Teacher teacher =(Teacher) session.get(Teacher.class,4L);
		session.delete(teacher);
		
		//-------------------------------------------------
		session.getTransaction().commit();
		session.close();//关闭session，释放资源
	}


	
	
}
